import Vue from 'vue'
import VueRouter from 'vue-router'  
import layout from '@/views/layout'
import HomePage from '@/views/layout/HomePage.vue'
import CategoryPage from '@/views/layout/CategoryPage.vue'
import CartPage from '@/views/layout/CartPage.vue'
import MinePage from '@/views/layout/MinePage.vue'
import login from '@/views/login'
import order from '@/views/order'
import pay from '@/views/pay'
import prodetail from '@/views/prodetail'
import search from '@/views/search'
import SearchList from '@/views/search/SearchList.vue'
import store from '@/store'
import leeAddress from '@/views/leeAddress'
import addAddress from '@/views/leeAddress/add.vue'
// import { component } from 'vue/types/umd'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: HomePage
      },
      {
        path: '/category',
        component: CategoryPage
      },
      {
        path: '/cart',
        component: CartPage
      },
      {
        path: '/mine',
        component: MinePage
      },

    ]
  },
  { path: '/login', component: login},
  { path: '/order', component: order},
  { path: '/pay', component: pay},
  { path: '/prodetail/:id', component: prodetail},
  { path: '/search', component: search},
  { path: '/searchList', component: SearchList},
  { path: '/leeAddress', component: leeAddress},
  { path: '/addAddress', component: addAddress}
  
]

// 定义需要登录后才能访问的页面
const rotersNeedLogin = [
  '/pay', '/cart', '/mine', '/order', '/leeAddress'
]


const router = new VueRouter({
  routes
})


// 配置路由前置守卫
router.beforeEach((to, from, next) => {
  
  // 游客态可访问的页面
  if(rotersNeedLogin.includes(to.path)) {
    // 判断当前是否登录
    const tokenStr = store.getters.token

    // console.log('路由前置守卫--- ' + to.path)
    // console.log(tokenStr)

    // console.log('路由前置守卫--- ' + tokenStr)

    if(!tokenStr) { // 非登录
      router.push('/login') 
      return
    }
  }
  next()
})


export default router
